package com.ruoyi.pad.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.pad.domain.KyDeviceInfo;
import com.ruoyi.pad.mapper.KyDeviceInfoMapper;
import com.ruoyi.pad.service.IKyDeviceInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 仪器基础信息Service业务层处理
 *
 *
 * @date 2023-01-09
 */
@Service
public class KyDeviceInfoServiceImpl implements IKyDeviceInfoService {
    @Resource
    private KyDeviceInfoMapper kyDeviceInfoMapper;

    /**
     * 查询仪器基础信息
     *
     * @param devId 仪器基础信息主键
     * @return 仪器基础信息
     */
    @Override
    public KyDeviceInfo selectKyDeviceInfoByDevId(Long devId) {
        return kyDeviceInfoMapper.selectKyDeviceInfoByDevId(devId);
    }

    /**
     * 查询仪器基础信息
     *
     * @param userId 用户id
     * @return 仪器基础信息
     */
    @Override
    public List<KyDeviceInfo> selectKyDeviceInfoByUserId(Long userId) {
        return kyDeviceInfoMapper.selectKyDeviceInfoByUserId(userId);
    }

    /**
     * 根据用户Id和设备num删除
     *
     * @param devNum
     * @param userId
     * @return
     */
    @Override
    public int deleteKyDeviceInfoByDevNum(String devNum, String userId) {
        return kyDeviceInfoMapper.deleteKyDeviceInfoByDevNum(devNum, userId);
    }

    /**
     * 查询仪器基础信息列表
     *
     * @param kyDeviceInfo 仪器基础信息
     * @return 仪器基础信息
     */
    @Override
    public List<KyDeviceInfo> selectKyDeviceInfoList(KyDeviceInfo kyDeviceInfo) {
        return kyDeviceInfoMapper.selectKyDeviceInfoList(kyDeviceInfo);
    }

    /**
     * 新增仪器基础信息
     *
     * @param kyDeviceInfo 仪器基础信息
     * @return 结果
     */
    @Override
    public int insertKyDeviceInfo(KyDeviceInfo kyDeviceInfo) {
        kyDeviceInfo.setCreateTime(DateUtils.getNowDate());
        return kyDeviceInfoMapper.insertKyDeviceInfo(kyDeviceInfo);
    }

    /**
     * 修改仪器基础信息
     *
     * @param kyDeviceInfo 仪器基础信息
     * @return 结果
     */
    @Override
    public int updateKyDeviceInfo(KyDeviceInfo kyDeviceInfo) {
        kyDeviceInfo.setUpdateTime(DateUtils.getNowDate());
        return kyDeviceInfoMapper.updateKyDeviceInfo(kyDeviceInfo);
    }

    /**
     * 批量删除仪器基础信息
     *
     * @param devIds 需要删除的仪器基础信息主键
     * @return 结果
     */
    @Override
    public int deleteKyDeviceInfoByDevIds(Long[] devIds) {
        return kyDeviceInfoMapper.deleteKyDeviceInfoByDevIds(devIds);
    }

    /**
     * 删除仪器基础信息信息
     *
     * @param devId 仪器基础信息主键
     * @return 结果
     */
    @Override
    public int deleteKyDeviceInfoByDevId(Long devId) {
        return kyDeviceInfoMapper.deleteKyDeviceInfoByDevId(devId);
    }



    /**
     *  根据”仪器编码“，查询仪器
     * @param devNum
     * @return
     */
    @Override
    public KyDeviceInfo selectKyDeviceInfoByDevNum(String devNum) {
        return kyDeviceInfoMapper.selectKyDeviceInfoByDevNum(devNum);
    }

    /**
     * 获取当前已绑定的仪器信息
     *
     * @param kyDeviceInfo
     */
    @Override
    public List<String> selectKyBindDevice(KyDeviceInfo kyDeviceInfo) {
        kyDeviceInfo.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        kyDeviceInfo.setDeptId(SecurityUtils.getLoginUser().getUser().getDeptId());
        List<KyDeviceInfo> list = selectKyDeviceInfoList(kyDeviceInfo);
        //获取用户绑定的app和dev
        List<String> apps = new ArrayList<>();
        list.forEach(item -> {
            apps.add(item.getDevType());
            if(item.getAppTypes() != null) {
                List strList2 = Arrays.stream(item.getAppTypes().split(",")).collect(Collectors.toList());
                apps.addAll(strList2);
            }
        });
        return apps.stream().distinct().collect(Collectors.toList());
    }

    @Override
    public int batchInsert(List<KyDeviceInfo> deviceInfoList) {
        return kyDeviceInfoMapper.batchInsert(deviceInfoList);
    }

    @Override
    public List<KyDeviceInfo> selectNewKyDeviceInfoList(Long deptId, Long startTime) {
        String time = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,new Date(startTime));
        List<KyDeviceInfo> deviceInfoList = kyDeviceInfoMapper.selectNewKyDeviceInfoList(deptId,time);
        return deviceInfoList;
    }


}
